$(document).ready(function(){
	const api = require("../modules/api.js");

	console.log(window.location.pathname);
	const search = {
		pageNo: 1,
		pageSize: 10,
		isTPOnly: window.location.pathname == "/h5/tp"
	};
	const product_temp = env.getTemplate("h5/product.html");
	let pageCount = 0;
	let is_loading = true;

	// 滚动
	$(window).scroll(function(){
		let screenHeight = $(this).height();
		let listCurrentHeight = $(".product-list").height();
		let scrollTop = $(this).scrollTop();
		if((scrollTop + screenHeight > listCurrentHeight) && is_loading){
			is_loading = false;
			if(search.pageNo < pageCount){
				search.pageNo += 1;
				setTimeout(()=>{
					getList();
				}, 200);

				$(".ending").hide();
				$(".loading").show();
			}else{
				$(".ending").show();
				$(".loading").hide();
			};
		};
	});
	
	getList();

	// 获取列表
	function getList(){
		api.product_query(search).then(res=>{
			if(res.code == "0"){
				console.log(res);
				let list = res.result.list || [];
				pageCount = res.result.page.pageCount;
				is_loading = true;
				if(search.pageNo > 1){
					render_list(list);
				};
			};
		});
	};

	// 渲染
	function render_list(list){
		list.map(item=>{
			product_temp.render({product: item}, (err, html)=>{
				$(".product-list").append(html);
			});
		});
	};
})